package com.example.com.common.view.dialog;


import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.com.common.R;


public class InputTextDialog extends BaseDialog {
    private boolean mIsCancle;
    View.OnClickListener leftBtnListener, rightBtnListener;
    String leftBtnText, rightBtnText;
    String title;
    CharSequence message;
    int gravity = Gravity.CENTER_VERTICAL;
    private EditText content;
    private final boolean mIsSingleLine;

    public InputTextDialog(Context context, CharSequence hintText, String title, boolean isSingleLine, boolean isCancel) {
        super(context, R.style.translucentDialog);
        this.title = title;
        this.message = hintText;
        mIsSingleLine = isSingleLine;
        mIsCancle = isCancel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_input_text);
        if (!TextUtils.isEmpty(title)) {
            ((TextView) findViewById(R.id.tv_dialog_title)).setText(title);
        } else {
            findViewById(R.id.tv_dialog_title).setVisibility(View.GONE);
            findViewById(R.id.dialog_title_line).setVisibility(View.GONE);
        }
        content = ((EditText) findViewById(R.id.et_dialog_content));
        if (!mIsSingleLine) {
            //是否为多行显示，默认为单行
            content.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
            content.setSingleLine(false);
        }
        content.setHint(message);
        content.setGravity(gravity);
        Button button = (Button) findViewById(R.id.btn_choose_left);
        if (!TextUtils.isEmpty(leftBtnText))
            button.setText(leftBtnText);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mIsCancle)
                    dismiss();

                if (leftBtnListener != null) {
                    leftBtnListener.onClick(v);
                }
            }
        });
        button = (Button) findViewById(R.id.btn_choose_right);
        if (!TextUtils.isEmpty(rightBtnText))
            button.setText(rightBtnText);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mIsCancle)
                    dismiss();

                if (rightBtnListener != null)
                    rightBtnListener.onClick(v);
            }
        });
        findViewById(R.id.linear_outside).setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (canceledOnTouchOutside)
                    dismiss();
                return false;
            }
        });

    }

    public void setLeftButton(String text,
                              View.OnClickListener listener) {
        leftBtnText = text;
        leftBtnListener = listener;
    }

    public void setRightButton(String text,
                               View.OnClickListener listener) {
        rightBtnText = text;
        rightBtnListener = listener;
    }

    public String getInputText() {
        return content.getText().toString();
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    private boolean canceledOnTouchOutside = true;

    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        this.canceledOnTouchOutside = cancel;
    }
}
